package dialog;

import java.awt.Container;
import java.awt.event.*;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;

import client.Client;

public class LoginDialog extends JFrame implements ActionListener{

    private Client client;
	private String _password=null,_server=null, _client = null;
	private JLabel label1,label2,label3,label4;
	private JTextField user,server,port;
	private JPasswordField password;
	private JButton ok,cancel;
	private Container container;
	final String 	SERVER_HOST 		= "Cameron";
	final int		CLIENT_PORT 		= 2011;

    /**
     * Create the frame.
     */
    public LoginDialog(Client theClient) {
        this.client = theClient;
		
		container = this.getContentPane();
		container.setLayout(null);
		label1= new JLabel("Login name :");
		label1.setBounds(10,10,80,20);
		label2= new JLabel("  Password :");
		label2.setBounds(10,40,80,20);
		user= new JTextField();
		user.setBounds(100,10,100,20);
		password=new JPasswordField("123");
		password.setBounds(100,40,100,20);

		label3= new JLabel("      Server :");
		label3.setBounds(10,70,80,20);
		label4= new JLabel("Client Port :");
		label4.setBounds(10,100,80,20);
		server= new JTextField(SERVER_HOST);
		server.setBounds(100,70,100,20);
		port=new JTextField(CLIENT_PORT+"");
		port.setBounds(100,100,100,20);
		port.setEditable(true);
		ok=new JButton("Login");
		ok.setBounds(30,130,70,20);
		cancel= new JButton("Cancel");
		cancel.setBounds(110,130,80,20);

		container.add(label1);
		container.add(user);
		container.add(label2);
		container.add(password);
		container.add(label3);
		container.add(server);
		container.add(label4);
		container.add(port);
		container.add(ok);
		container.add(cancel);

		user.addActionListener(this);
		password.addActionListener(this);
		server.addActionListener(this);
		port.addActionListener(this);
		ok.addActionListener(this);
		cancel.addActionListener(this);

		this.setSize(220,190);
		this.setResizable(false);
//		this.setLocationRelativeTo(frame);
		this.setLocation(450,220);
		this.setVisible(true);
    }
	public void actionPerformed(ActionEvent event)
	{
        if((event.getSource() == ok) || (event.getSource() == password)
        	||(event.getSource() == user) || (event.getSource() == server) ) {
			client.userName = user.getText();
			client.password = new String(password.getPassword());
			_server = server.getText();
			_client = port.getText();
			client.register();
			
			if((client.userName.length()==0) || (client.password.length() == 0))
			{
				JOptionPane.showMessageDialog(this,
					"Please enter a valid username and password","Error",
						JOptionPane.WARNING_MESSAGE);
				return;
			}
			if(_server.length()== 0)
			{
				JOptionPane.showMessageDialog(this,
					"Invalid server host","Error",
						JOptionPane.WARNING_MESSAGE);
				return;
			}

		} else if(event.getSource() == cancel) {
			System.exit(0);
		}
		this.setVisible(false);

	}

}
